﻿/*
 ***********************************
 时间：2017.9.22
 代码：WangSir
 脚本作用：奖励界面   去评价界面管理
 ************************************
 */
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using WangSir;
using UnityEngine.UI;

/// <summary> </summary>
public class EvaluateAwardController : MonoBehaviour
{
    public static EvaluateAwardController Instance;

    private GameObject EvaluateDialog;
    [HideInInspector]
    public GameObject AwardDialog;
    private GameObject ResurgenceDialog;
    private GameObject SeeVideoDialog;
    private Text NotReadyDialog;

    private Text m_AwardText;
    public int getInGameNum;
    public GameObject lineBtns;
    private void Awake()
    {
        if (Instance == null) Instance = this;
        else if (Instance != this) Destroy(gameObject);
        DontDestroyOnLoad(gameObject);
    }

    void Start()
    {
        EvaluateDialog = Global.FindChild(transform, "EvaluateDialog");
        AwardDialog = Global.FindChild(transform, "AwardDialog");
        ResurgenceDialog = Global.FindChild(transform, "ResurgenceDialog");
        m_AwardText = Global.FindChild<Text>(transform, "Award_Text");
        SeeVideoDialog = Global.FindChild(transform, "SeeVideoDialog");
        NotReadyDialog = Global.FindChild<Text>(transform, "NotReadyDialog");
        getInGameNum = PlayerPrefs.GetInt("GetInNum", 0);
        getInGameNum++;
        PlayerPrefs.SetInt("GetInNum", getInGameNum);

        Global.FindChild(transform, "WatchVideo").GetComponent<Button>().onClick.AddListener(WatchVideo);
        Global.FindChild(transform, "328").GetComponent<Button>().onClick.AddListener(delegate { BuyProduct(2); });
        Global.FindChild(transform, "restore").GetComponent<Button>().onClick.AddListener(delegate { RestoreProduct(2); });
    }

    void Update()
    {
        if (Beyond.Ads.isReady(Beyond.Ads.Type.video) && !GameState.isVideoReady)
        {
            GameState.isVideoReady = true;
        }
    }

    public void WatchVideo()
    {
        if (Beyond.Ads.isReady(Beyond.Ads.Type.video) == true)
        {
            AdsProxy.PlayVideoByType(AdsProxy.VideoType.VideoToGetRuby);
        }
        else
        {
            StartCoroutine(waitdoto());
        }
    }

    /// <summary>打开去评价界面</summary>
    public void OpenEvaluateDialog()
    {
        EvaluateDialog.SetActive(true);
    }

    /// <summary>关闭去评价界面</summary>
    public void CancelEvaluateDialog()
    {
        EvaluateDialog.SetActive(false);
    }

    /// <summary>打开奖励界面</summary>
    public void OpenAwardDialog(string awardContents)
    {
        AwardDialog.SetActive(true);
        if(Beyond.AppInfo.getChannelId()==66000)
        {
            m_AwardText.color = new Vector4(1, 1, 1, 1);
        }
        m_AwardText.text = awardContents;
    }

    /// <summary>关闭奖励界面</summary>
    public void CancelAwardDialogDialog()
    {
        AwardDialog.SetActive(false);
        Time.timeScale = 1;
    }

    /// <summary>打开复活界面</summary>
    public void OpenResurgenceDialog()
    {
        if (Beyond.Ads.isReady(Beyond.Ads.Type.video) == true)
        {
            ResurgenceDialog.SetActive(true);
        }
        else
        {
            
        }
        GameState.isResDialogShow = true;
    }

    /// <summary>取消复活界面</summary>
    public void CancelResurgenceDialog()
    {
        ResurgenceDialog.SetActive(false);
    }

    /// <summary>关闭复活界面</summary>
    public void CloseResurgenceDialog()
    {
        ResurgenceDialog.SetActive(false);
    }

    /// <summary>打开看视频界面</summary>
    public void OpenSeeVideoDialog()
    {
        SeeVideoDialog.SetActive(true);
    }

    /// <summary>关闭看视频界面</summary>
    public void CloseSeeVideoDialog()
    {
        SeeVideoDialog.SetActive(false);
    }

    public void UnlockTeam()
    {

        if (GameState.isVideoReady)
        {

        }
        else
        {
            StartCoroutine(waitdoto());
        }

        GameState.isVideoReady = false;
    }

    IEnumerator waitdoto()
    {
        NotReadyDialog.gameObject.SetActive(true);
        yield return new WaitForSeconds(1f);
        NotReadyDialog.gameObject.SetActive(false);
        NotReadyDialog.color = new Vector4(1, 1, 1, 1);
    }

    public void BuyProduct(int goodId)
    {
        PurchaseProxy.pay(goodId);
    }

    public void RestoreProduct(int goodId)
    {
        PurchaseProxy.restore(goodId);
    }
}